package com.kuang.demo.controller;

import com.kuang.demo.bean.BaseRespVo;
import com.kuang.demo.bean.GoodsBean.GoodsBO.AddGoods;
import com.kuang.demo.bean.GoodsBean.GoodsBO.CatAndBrandBO;
import com.kuang.demo.bean.GoodsBean.GoodsBO.GoodsAddList;
import com.kuang.demo.bean.GoodsBean.GoodsBO.GoodsCommentDelete;
import com.kuang.demo.bean.GoodsBean.GoodsVO.FileUpLoadGoodsPicVO;
import com.kuang.demo.bean.GoodsBean.GoodsVO.GoodsCommetVO;
import com.kuang.demo.bean.GoodsBean.GoodsVO.GoodsDetailVO;
import com.kuang.demo.bean.GoodsBean.GoodsVO.GoodsListVO;
import com.kuang.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * goodsList 查询
 * comment 查询
 * comment 回复
 * admin/StorageMapper/create
 */
@RestController
@RequestMapping("admin")
public class GoodsController {
    /**
     * GOODS LIST
     */
    @Autowired
    GoodsService goodsService;

//    @GetMapping("goods/list")
//    public GoodsListVO goodsList0(int page, int limit, String sort, String order) {
//        GoodsListVO goodsListVO = new GoodsListVO();
//        goodsListVO = goodsService.goodsList(goodsListVO, page, limit, sort, order);
//        return goodsListVO;
//    }
//
//    /**
//     * page=1&limit=20&goodsSn=1&name=1&sort=add_time&order=desc
//     */
//    @GetMapping("goods/list")
//    public GoodsListVO goodsList1(int page, int limit, String sort, String order, String goodsSn) {
//        GoodsListVO goodsListVO = new GoodsListVO();
//        goodsListVO = goodsService.goodsList(goodsListVO, page, limit, sort, order,goodsSn);
//        return goodsListVO;
//    }
    @GetMapping("goods/list")
    public GoodsListVO goodsList(int page, int limit, String sort, String order, String goodsSn,String name) {
        GoodsListVO goodsListVO = new GoodsListVO();
        goodsListVO = goodsService.goodsList(goodsListVO, page, limit, sort, order,goodsSn,name);
        return goodsListVO;
    }

    /**
     * 添加商品的类别和标签
     */
    @GetMapping("goods/catAndBrand")
    public CatAndBrandBO catAndBrand() {
        CatAndBrandBO catAndBrandBO = new CatAndBrandBO();
        catAndBrandBO = goodsService.catAndBrand(catAndBrandBO);
        return catAndBrandBO;
    }

    @PostMapping("goods/create")
    public BaseRespVo addGoods(@RequestBody GoodsAddList goodsAddList) {
        goodsService.addGoods(goodsAddList);
        return BaseRespVo.ok();
    }

    @PostMapping("goods/delete")
    public BaseRespVo deleteGoodsList(@RequestBody AddGoods addGoods) {
        goodsService.deleteGoodsList(addGoods);
        return BaseRespVo.ok();
    }

    @GetMapping("goods/detail")
    public BaseRespVo selectGoodsDetailById(int id) {
        GoodsDetailVO goodsDetailVO = goodsService.selectGoodsDetailById(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    @PostMapping("goods/update")
    public BaseRespVo updateGoodsDetail(@RequestBody GoodsDetailVO goodsDetailVO) {
        goodsService.updateGoodsDetail(goodsDetailVO);
        return BaseRespVo.ok();
    }



    @PostMapping("storage/create")
    public BaseRespVo createPic(MultipartFile file, HttpServletRequest request) throws IOException {
        StringBuffer requestURL = request.getRequestURL();
        String url = String.valueOf(requestURL).replace("storage/create", "");
        FileUpLoadGoodsPicVO fileUpLoadGoodsPicVO = goodsService.createPic(url, file);
        return BaseRespVo.ok(fileUpLoadGoodsPicVO);
    }
}
